package com.xl333.calc;

import java.math.BigDecimal;

/**
 * @auth liux
 * @date 2021/6/18
 */
public class Calc {

    /**
     * 采用StringBuilder类型，便于截取操作
     */
    private StringBuilder firstNum;

    private StringBuilder secondNum;

    private OperatorEnum operator;

    @Override
    public String toString(){
        if(this.firstNum == null){
            return "0";
        }else if(this.operator == null){
            return this.firstNum.toString();
        }else if(this.secondNum == null){
            return new StringBuilder().append(this.firstNum).append(this.operator.getOperator()).toString();
        }else{
            return new StringBuilder().append(this.firstNum).append(this.operator.getOperator()).append(this.secondNum).toString();
        }
    }

    /**
     * 输入一个数字
     * @param i
     */
    public void inputNumber(int i){
        if(operator == null){
            this.firstNum = appendNumber(this.firstNum, i);
        }else{
            this.secondNum = appendNumber(this.secondNum, i);
        }
    }

    private StringBuilder appendNumber(StringBuilder builder, int i){
        if(builder == null){
            builder = new StringBuilder();
        }
        builder.append(i);
        return builder;
    }

    /**
     * 输入操作符号
     * @param operatorEnum
     */
    public void inputOperator(OperatorEnum operatorEnum){
        switch(operatorEnum){
            case PERCENT:
                percent();
                return;
            case CE:
                ce();
                return;
            case C:
                c();
                return;
            case DELETE:
                delete();
                return;
            case ADD:
                add();
                return;
            case SUBTRACT:
                subtract();
                return;
            case MULTIPLY:
                multiply();
                return;
            case DIVIDE:
                divide();
                return;
            case EQUAL:
                equal();
                return;
            case POINT:
                point();
        }
    }

    private BigDecimal toBigDecimal(StringBuilder builder){
        return new BigDecimal(builder.toString());
    }

    private boolean isEmpty(StringBuilder builder){
        return builder == null || builder.length() <= 0;
    }

    /**
     * 计算结果
     */
    private void calcResult(){
        if(isEmpty(this.secondNum)){
            return;
        }

        switch (this.operator){
            case ADD:
                this.firstNum = new StringBuilder(toBigDecimal(this.firstNum).add(toBigDecimal(this.secondNum)).toString());
                break;
            case SUBTRACT:
                this.firstNum = new StringBuilder(toBigDecimal(this.firstNum).subtract(toBigDecimal(this.secondNum)).toString());
                break;
            case MULTIPLY:
                this.firstNum = new StringBuilder(toBigDecimal(this.firstNum).multiply(toBigDecimal(this.secondNum)).toString());
                break;
            case DIVIDE:
                this.firstNum = new StringBuilder(toBigDecimal(this.firstNum).divide(toBigDecimal(this.secondNum))
                        .setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        }

        this.operator = null;
        this.secondNum = null;
    }

    /**
     * 百分比按钮
     */
    private void percent(){
        if(isEmpty(this.firstNum)){
            return;
        }else{
            calcResult();
            this.firstNum = new StringBuilder(toBigDecimal(this.firstNum).divide(BigDecimal.valueOf(100)).toString());
            this.operator = null;
        }
    }

    /**
     * CE按钮
     */
    private void ce(){
        if(!isEmpty(this.secondNum)){
            this.secondNum = null;
        }else if(!isEmpty(this.firstNum) && this.operator == null){
            this.firstNum = null;
        }
    }

    /**
     * C按钮
     */
    private void c(){
        this.firstNum = null;
        this.secondNum = null;
        this.operator = null;
    }

    /**
     * 删除按钮
     */
    private void delete(){
        if(!isEmpty(this.secondNum)){
            this.secondNum.delete(this.secondNum.length() - 1, this.secondNum.length());
        }else if(!isEmpty(this.firstNum)){
            this.firstNum.delete(this.firstNum.length() - 1, this.firstNum.length());
        }
    }

    /**
     * 加法按钮
     */
    private void add(){
        if(!isEmpty(this.secondNum)){
            calcResult();
        }
        this.operator = OperatorEnum.ADD;
    }

    /**
     * 减法按钮
     */
    private void subtract(){
        if(!isEmpty(this.secondNum)){
            calcResult();
        }
        this.operator = OperatorEnum.SUBTRACT;
    }

    /**
     * 乘法按钮
     */
    private void multiply(){
        if(!isEmpty(this.secondNum)){
            calcResult();
        }
        this.operator = OperatorEnum.MULTIPLY;
    }

    /**
     * 除法按钮
     */
    private void divide(){
        if(!isEmpty(this.secondNum)){
            calcResult();
        }
        this.operator = OperatorEnum.DIVIDE;
    }

    /**
     * 等号按钮
     */
    private void equal(){
        calcResult();
    }

    /**
     * 小数点按钮
     */
    private void point(){
        if(this.operator == null){
            this.firstNum = appendPoint(this.firstNum);
        }else{
            this.secondNum = appendPoint(this.secondNum);
        }
    }

    /**
     * 拼接小数点
     * @param builder
     * @return
     */
    private StringBuilder appendPoint(StringBuilder builder){
        if(isEmpty(builder)){
            builder = new StringBuilder("0.");
        }else if(builder.indexOf(".") <= 0){
            builder.append(".");
        }
        return builder;
    }

}
